 
import { build as vite_build  } from 'vite'
import { build as vitepress_build } from 'vitepress'

import fsp from "fs/promises"  
import path  from 'path'
 
import electron_builder  from "electron-builder"   //全局包  当前目录执行  npm link electron-builder       
import _ from "lodash-es"
 
// const __file = /(?<=file:\/\/).*/g.exec(decodeURI(import.meta.url))[0] 
const __file     = decodeURI(new URL(import.meta.url).pathname)
const __dirname  = decodeURI(new URL("./", import.meta.url).pathname) 
const 项目输出目录 = decodeURI(new URL("./dist", import.meta.url).pathname)
 


var 版本号 = `${(new Date().getFullYear()).toString().substring(2)}.${new Date().getMonth()+1}.${new Date().getDate()}` .trim()
var 预发布版本 = "beta" // 通用node方法.一年中第几天(new Date())  
var 完整版本号 = `${版本号}-${预发布版本}` 


var 打包方式 = _.last(process.argv) ==="发布" ? {} : {compression:"store"}
process.env.打包调试 = true

async function 打包应用app(){
    let 计时 = Date.now()
    await fsp.rm(项目输出目录,{force:true,recursive:true})
      
    process.env.ELECTRON_MIRROR  = "http://npmmirror.com/mirrors/electron/"
    process.env.NPM_CONFIG_ELECTRON_BUILDER_BINARIES_MIRROR = "http://npmmirror.com/mirrors/electron-builder-binaries/"
    //自动下载 fpm
    await electron_builder.build({
        projectDir:__dirname,
        targets: electron_builder.Platform.LINUX.createTarget(),
        config: {
            electronDownload: { mirror: "http://npmmirror.com/mirrors/electron/", },
            // compression:"store",                                                           // store 体积大 但速度最快  30秒打包完成
            directories: { output: 项目输出目录  },   
            asar:true,                 
            asarUnpack:["node_modules/node-pty"],                                 //复制一份,原有的asar中同样有一份
            extraResources:[                                                      //指的是==>  /opt/文件浏览器/resources   
                
            ],
            files: [                                       //    "**/*",  全部文件
                "src",
            ],
            //includeSubNodeModules:true,                   // 🔥🔥🔥 这个导致打包错误 
            electronLanguages:"zh-CN",                      //支持的语言
            appId: "lwleen-test",
            productName:"electron空白应用",
            copyright:"GPL-3.0",         
            artifactName: "lwleen-test_"+完整版本号+"_${arch}.${ext}",  //最终 deb 文件名  -${os}
            //🍍🍍🍍深度链接(Deep Links)  系统级别唤醒应用
            protocols:[{name:"electron空白应用", schemes:["lwleentest"], role:"Viewer"} ],  
            linux: {
                icon:"./图标",
                desktop: {
                    "Package":"lwleen-test",
                    "Version": 完整版本号,
                    "Architecture":"amd64",
                    "Maintainer":'一切时空过去未来',
                    "Description":"electron空白应用",
                    "Encoding": "UTF-8",
                    "MimeType": " ",
                    "Name":"electron空白应用",
                    "Name[C]":"electron空白应用",
                    "Name[zh.]":"electron空白应用",
                    "Name[zh]":"electron空白应用",
                    "Name[zh_CN]":"electron空白应用",
                },
                mimeTypes:[ ],
                category: "System;Utility",
                maintainer:"一切时空过去未来",
                description:"_你好, electron",
                target: {target:"deb",arch:"x64" },

                // customInstallPath: "/opt/apps",
            },
            deb: {
                // depends:["smbclient","ffmpeg"],     // "ffmpeg","smbclient","arp","df","du"
                // fpm:["--after-install=src/deb-preinstall.sh"],
                //afterRemove:""
            }, 
        },
    })


    console.log(`\n🔥 打包成功: ${版本号}  用时 ${parseInt(((Date.now() - 计时)/1000)/60)}分 ${parseInt(((Date.now() - 计时)/1000)%60)}秒 🔥\n`)
}
 

 
//--------主动打包---------------
打包应用app() 